﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalWareHousing
    {
        //Them mot phieu nhap kho
        public static int addWareHousing(BizWareHousing pWareHousing)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                if (isExistWareHousingCharid(pWareHousing.warehousing_charid) == false) //Neu ma phieu nhap kho chua ton tai
                {
                    Warehousing wareHousing = new Warehousing();
                    wareHousing.WarehousingCharID = pWareHousing.warehousing_charid;
                    wareHousing.DateTimeWarehousing = pWareHousing.datetime_warehousing;
                    wareHousing.DateTimePhoneWarehousing = pWareHousing.datetime_phone_warehousing;
                    wareHousing.ByNA = pWareHousing.by_na;
                    wareHousing.ByShop = pWareHousing.by_shop;
                    db.Warehousing.InsertOnSubmit(wareHousing);
                    db.SubmitChanges();
                    return 1;
                }
                return 0;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        //Kiem tra ma phieu nhap kho co ton tai
        public static bool isExistWareHousingCharid(string pWarehousing_charid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            var wareHousings = from item in db.Warehousing
                               where item.WarehousingCharID == pWarehousing_charid
                               select item;
            if(wareHousings.Count() == 0)
                return false;
            return true;
        }

        #region Dung cho Webservice
        //Them phieu nhap kho boi NA
        public static int addWareHousingbyBA(BizWareHousing pWareHousing, BizWareHousingDetail pWareHousingDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                bool status = isExistWareHousingCharid(pWareHousing.warehousing_charid);
                if (status == false) //Neu chua ton tai phieu nhap kho
                    pWareHousing.addWareHousing();

                pWareHousingDetail.addWareHousingDetail();
                //Cap nhat lai so luong san pham trong cua hang
                BizProductInShop.updateProductQuantityInShop(pWareHousingDetail.by_product, pWareHousing.by_shop, pWareHousingDetail.quantity);

                //Cap nhat lai so luong san pham trong nhat ki hang
                BizProductInShopDiary.updateProductQuantityInShopDiary(pWareHousingDetail.by_product, pWareHousing.by_shop, pWareHousing.datetime_phone_warehousing, pWareHousingDetail.quantity);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        #endregion
    }
}
